package top.shen33.yang.services.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.shen33.yang.mapper.EdoPostMapper;
import top.shen33.yang.model.entity.EdoCate;
import top.shen33.yang.model.entity.EdoPost;
import top.shen33.yang.model.entity.EdoTag;
import top.shen33.yang.model.entity.EdoTagPostRel;
import top.shen33.yang.services.EdoPostService;

import java.util.List;

/**
 * <p>
 * 正文 服务实现类
 * </p>
 *
 * @author oreilly
 * @since 2018-08-30
 */
@Service
public class EdoPostServiceImpl extends ServiceImpl<EdoPostMapper, EdoPost>implements EdoPostService {


    @Override
    public Class clazz() {
        return EdoPost.class;
    }

    @Override
    @Transactional
    public void doAdd(EdoPost itemEdoPost) {

        itemEdoPost.insertOrUpdate();

        //数量
        Integer cateId = itemEdoPost.getCateId();
        EdoCate edoCate = new EdoCate().selectById(cateId);
        Integer postNum = edoCate.getPostNum();
        if(postNum == null){
            postNum = 0;
        }
        postNum++;
        edoCate.setPostNum(postNum);
        edoCate.insertOrUpdate();


        updateTagRel(itemEdoPost);




    }


    private EdoTag getOrNew(String tagName) {
        QueryWrapper<EdoTag> wrapper = new QueryWrapper<>();
        wrapper.eq("tag_name",tagName);

        EdoTag edoTag = new EdoTag().selectOne(wrapper);

        //没有就 新增一个 tag
        if(edoTag == null){
            EdoTag edoTag1 = new EdoTag();
            edoTag1.setTagName(tagName);
            edoTag1.insertOrUpdate();
            edoTag = edoTag1;
        }
        return edoTag;
    }


    @Override
    @Transactional
    public void doUpdate(EdoPost itemEdoPost) {

        itemEdoPost.insertOrUpdate();

        updateTagRel(itemEdoPost);
    }

    private void updateTagRel(EdoPost itemEdoPost) {
        Integer postSid = itemEdoPost.getSid();

        //清空原来的关联
        EdoTagPostRel rel = new EdoTagPostRel();
        rel.delete(new QueryWrapper<EdoTagPostRel>().eq("post_id",postSid));

        //建立新的 关联
        String tags = itemEdoPost.getTags();
        if(StringUtils.isNotBlank(tags)){

            String[] tagNameArr = tags.split("[,]");
            for (String tagName : tagNameArr) {

                EdoTag edoTag = getOrNew(tagName);
                Integer tagSid = edoTag.getSid();

                EdoTagPostRel tpRel = new EdoTagPostRel();
                tpRel.setPostId(postSid);
                tpRel.setTagId(tagSid);
                tpRel.insertOrUpdate();

            }
        }
    }

    @Override
    @Transactional
    public void doDelete(String[] ids) {

        QueryWrapper<EdoPost> ew = new QueryWrapper<>();
        ew.in("sid", ids);
        new EdoPost().delete(ew);
    }

    @Override
    public EdoPost getOne(String sid) {

        //关联查询
//        QueryWrapper<EdoPostReply> wrapper = new QueryWrapper<>();
//        wrapper.eq("post_id",sid);
//        EdoPostReply edoPostReply = new EdoPostReply();
//        List<EdoPostReply> replyList = edoPostReply.selectList(wrapper);

        EdoPost itemEdoPost = getById(sid);
//        itemEdoPost.setReplyList(replyList);

        return itemEdoPost;
    }

    @Override
    public List<EdoPost> all(EdoPost itemEdoPost) {

        return this.baseMapper.all(itemEdoPost);
    }


        @Override
    public Page<EdoPost> pageInfo(Page page, EdoPost itemEdoPost) {

        List<EdoPost> dataList = this.baseMapper.pageInfo(page, itemEdoPost);
        if(page ==null ){
            page = new Page(0,10000);
        }
        page.setRecords(dataList);
        return page;
    }


    @Override
    @Transactional
    public void doImport(List<EdoPost> dataList) {

        //数据验证

        //保存
        this.saveOrUpdateBatch(dataList);
    }

}
